package web;

import factory.ServiceFactory;
import pojo.po.User;
import pojo.vo.PageBean;
import pojo.vo.Pagination;
import pojo.vo.Result;
import service.UserService;
import service.UserServiceImpl;
import utils.BaseController;

import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "/user/*", value = "/user/*")
public class UserServlet extends BaseServlet {
    UserService service =(UserService) ServiceFactory.getBean("userService");
/**
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String[] split = requestURI.split("/");
        String method = split[split.length - 1];
        System.err.println("method:" + method);

//        switch (method){
//            case "FindAllUsers":
//                FindAllUsers(request,response);
//                break;
//            case "deleteUserById":
//                deleteUserById(request,response);
//                break;
//        }
        if (method.equals("deleteUserById")) {
            deleteUserById(request, response);
        } else if (method.equals("FindAllUsers")) {
            FindAllUsers(request, response);
        }else if(method.equals("updateUser")){
            updateUser(request,response);
        }else if(method.equals("addUser")){
            addUser(request,response);
        }else if(method.equals("findUserByLike")){
            findUserByLike(request,response);
        }
    }
    //模糊查询
    private void findUserByLike(HttpServletRequest request, HttpServletResponse response) {
        try {
            Pagination page = BaseController.parseJSON2Object(request, Pagination.class);
            System.out.println(page);
            Result result = service.findUserByLike(page);
            BaseController.printResult(response,result);

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
*/
    private void addUser(HttpServletRequest request, HttpServletResponse response) {
        try {
            User user = BaseController.parseJSON2Object(request, User.class);
            System.out.println(user);
            int r = service.addUser(user);
            Result result = new Result();
            if(r==1){
                System.out.println("添加成功");
                result.setMsg("添加成功！");
                result.setFlag(true);
            }else {
                result.setMsg("添加失败");
                result.setFlag(false);
            }
            BaseController.printResult(response,result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void updateUser(HttpServletRequest request, HttpServletResponse response) {
        try {
            User user = BaseController.parseJSON2Object(request, User.class);
            System.out.println(user);
            int r = service.updateUser(user);
            Result result = new Result();
            if(r==1){
                result.setMsg("修改成功！");
                result.setData(true);
            }else {
                result.setMsg("修改失败");
                result.setFlag(false);
            }
            BaseController.printResult(response,result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void deleteUserById(HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String id = request.getParameter("id");
        int r = service.deleteUserById(id);
        if (r == 1) {
            result.setFlag(true);
            result.setMsg("删除成功！");
        } else if (r == 0) {
            result.setFlag(true);
            result.setMsg("删除成功！（该用户无角色信息）");
        } else if (r == -1) {
            result.setFlag(false);
            result.setMsg("删除失败");
        }
        try {
            BaseController.printResult(response, result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void FindAllUsers(HttpServletRequest request, HttpServletResponse response) {
        Pagination page = null;
        try {
            page = BaseController.parseJSON2Object(request, Pagination.class);
            System.out.println(page);
            PageBean pageBean = service.FindAllUser(page);
            Result result = new Result();
            result.setData(pageBean);
            result.setFlag(true);
            result.setMsg("共搜索到"+pageBean.getTotal()+"条结果");
            BaseController.printResult(response, result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
